package viewset.com.kkcamera.view.activity

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.support.v4.app.ActivityCompat
import android.support.v4.content.PermissionChecker
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.widget.Button
import butterknife.BindView
import com.chan.mediacamera.GLCameraActivity
import kotlinx.android.synthetic.main.activity_main.*
import viewset.com.kkcamera.R
import viewset.com.kkcamera.view.image.opengl.EgTexture2dActivity
import viewset.com.kkcamera.view.image.opengl.OpenglActivity
import viewset.com.kkcamera.view.image.opengl.Texture2dFilterActivity

class MainActivity : AppCompatActivity(), View.OnClickListener {

    internal var btCamera: Button? = null

    internal var btVideo: Button? = null

    internal var btMagic: Button? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        button_glcamera.setOnClickListener(this@MainActivity)
        button_video_clip.setOnClickListener(this@MainActivity)
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.button_camera -> if (PermissionChecker.checkSelfPermission(this@MainActivity, Manifest.permission.CAMERA) == PackageManager.PERMISSION_DENIED || PermissionChecker.checkSelfPermission(this@MainActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
                ActivityCompat.requestPermissions(this@MainActivity, arrayOf(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE),
                        1)
            } else {
                startActivity(Intent(this, CameraActivity::class.java))
            }
            R.id.button_video -> {
            }
            R.id.button_magic -> if (PermissionChecker.checkSelfPermission(this@MainActivity, Manifest.permission.CAMERA) == PackageManager.PERMISSION_DENIED || PermissionChecker.checkSelfPermission(this@MainActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
                ActivityCompat.requestPermissions(this@MainActivity, arrayOf(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE),
                        2)
            } else {
                startActivity(Intent(this, MagicImgActivity::class.java))
            }
            R.id.button_magic_camera -> if (PermissionChecker.checkSelfPermission(this@MainActivity, Manifest.permission.CAMERA) == PackageManager.PERMISSION_DENIED || PermissionChecker.checkSelfPermission(this@MainActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
                ActivityCompat.requestPermissions(this@MainActivity, arrayOf(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE),
                        2)
            } else {
                startActivity(Intent(this, MagicCameraActivity::class.java))
            }
            R.id.button_opengl -> startActivity(Intent(this, OpenglActivity::class.java))
            R.id.button_texture2d -> startActivity(Intent(this, EgTexture2dActivity::class.java))
            R.id.button_texture2dfilter -> startActivity(Intent(this, Texture2dFilterActivity::class.java))
            R.id.button_glcamera -> if (PermissionChecker.checkSelfPermission(this@MainActivity, Manifest.permission.CAMERA) == PackageManager.PERMISSION_DENIED || PermissionChecker.checkSelfPermission(this@MainActivity, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED || PermissionChecker.checkSelfPermission(this@MainActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED || PermissionChecker.checkSelfPermission(this@MainActivity, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_DENIED) {
                ActivityCompat.requestPermissions(this@MainActivity, arrayOf(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO),
                        3)
            } else {
                startActivity(Intent(this, GLCameraActivity::class.java))
            }
            R.id.button_video_clip -> startActivity(Intent(this, VideoClipActivity::class.java))
        }
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>,
                                            grantResults: IntArray) {
        if (grantResults.size != 1 || grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            when (requestCode) {
                1 -> startActivity(Intent(this, CameraActivity::class.java))
                2 -> startActivity(Intent(this, MagicImgActivity::class.java))
                3 -> startActivity(Intent(this, GLCameraActivity::class.java))
            }

        } else {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        }
    }

    protected override fun onDestroy() {
        super.onDestroy()
    }
}